package com.pri.chat.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.netease.nim.uikit.api.NimUIKit;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.RequestCallbackWrapper;
import com.netease.nimlib.sdk.ResponseCode;
import com.netease.nimlib.sdk.msg.MsgService;
import com.netease.nimlib.sdk.msg.attachment.MsgAttachment;
import com.netease.nimlib.sdk.msg.constant.MsgStatusEnum;
import com.netease.nimlib.sdk.msg.constant.MsgTypeEnum;
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
import com.netease.nimlib.sdk.msg.model.RecentContact;
import com.pri.chat.R;
import com.pri.chat.nim.ChatListAdapter;
import com.pri.chat.nim.NearActiviy;
import com.pri.chat.utils.GlideImageLoader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.vondear.rxui.fragment.FragmentLazy;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 描述：添加类的描述
 *
 * @author wx5e48d604c2885118
 * @time 2020/4/3
 */
public class MessageFragment extends FragmentLazy {
    Unbinder unbinder;
    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.rec_main)
    RecyclerView recMain;
    @BindView(R.id.none)
    ImageView none;
    @BindView(R.id.srl_message)
    SmartRefreshLayout srlMessage;

    @Override
    protected View initViews(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstanceState) {
        View view = layoutInflater.inflate(R.layout.fragment_message_layout, viewGroup, false);
        unbinder = ButterKnife.bind(this, view);
        initRv();
        initBanner();
        initSrl();
        return view;
    }

    private void initBanner() {
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {

            }
        }).setImageLoader(new GlideImageLoader());
    }

    private void initSrl() {

    }
    private List<RecentContact> mlist = new ArrayList<>();
    private ChatListAdapter mAdapter;
    private void initRv() {
        LinearLayoutManager linearLayoutManager =new LinearLayoutManager(mContext);
        recMain.setLayoutManager(linearLayoutManager);
        recMain.setNestedScrollingEnabled(false);
        mAdapter=new ChatListAdapter(R.layout.item_chat_list,mlist);
        recMain.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                NimUIKit.startP2PSession(mContext, mlist.get(position).getContactId());
            }
        });
    }

    @Override
    protected void initData() {
        // 查询最近联系人列表数据
        NIMClient.getService(MsgService.class).queryRecentContacts().setCallback(
                new RequestCallbackWrapper<List<RecentContact>>() {

                    @Override
                    public void onResult(int code, List<RecentContact> recents, Throwable exception) {
                        if (code != ResponseCode.RES_SUCCESS || recents == null) {
                            return;
                        }
                        for (RecentContact loadedRecent : recents) {
                            if (loadedRecent.getSessionType() == SessionTypeEnum.P2P) {
                                mlist.add(loadedRecent);
                            }
                        }
                        mAdapter.notifyDataSetChanged();
                    }
                });
    }
}
